﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Best.SmartCms.Utility
{
    /// <summary>
    /// 枚举帮助类
    /// </summary>
    public class EnumHelper
    {
        /// <summary>
        /// 获取枚举值
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="desc"></param>
        /// <returns></returns>
        public static int GetValue<TEnum>(string desc)
        {
            var enumType = typeof(TEnum);
            var values = Enum.GetValues(enumType);

            foreach (var item in values)
            {
                var fieldInfo = enumType.GetField(item.ToString());
                if (fieldInfo == null) return -1;
                if (Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) is DescriptionAttribute attr)
                {
                    if (attr.Description == desc)
                    {
                        return (int)item;
                    }
                }
            }

            return -1;
        }
        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(Enum value)
        {
            var enumType = value.GetType();
            var name = Enum.GetName(enumType, value);
            if (name == null) return null;
            var fieldInfo = enumType.GetField(name);
            if (fieldInfo == null) return null;
            if (Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) is DescriptionAttribute attr)
            {
                return attr.Description;
            }
            return null;
        }

        /// <summary>
        /// 获取枚举值list信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumberEntity> EnumToList<T>()
        {
            var type = typeof(T);
            var list = GetEnumList(type);
            return list;
        }


        public static List<EnumberEntity> GetEnumList(Type type)
        {
            var list = new List<EnumberEntity>();
            foreach (var e in Enum.GetValues(type))
            {
                var m = new EnumberEntity();
                var objArr = e.GetType().GetField(e.ToString())
                    .GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr.Length > 0)
                {
                    if (objArr[0] is DescriptionAttribute da) m.Desction = da.Description;
                }

                m.EnumValue = Convert.ToInt32(e);
                m.EnumName = e.ToString();
                list.Add(m);
            }

            return list;
        }

       

        public class EnumberEntity
        {
            /// <summary>
            /// 枚举的描述
            /// </summary>
            public string Desction { set; get; }

            /// <summary>
            /// 枚举名称
            /// </summary>
            public string EnumName { set; get; }

            /// <summary>
            /// 枚举对象的值
            /// </summary>
            public int EnumValue { set; get; }
        }


    }
}
